package cn.buildoncloud.labor.workermanapplet.business.modules.controller;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.UserService;
import cn.buildoncloud.labor.workermanapplet.common.Constant;
import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.util.HttpUtil;
import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Value("${webSystemIP}")
    private String webSystemIP;

    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public Object getUser(@RequestParam String userId){

        try {
            Map<String, Object> userMap = userService.getUserByOpenId(userId);
            return Result.success(Constant.API_OPERATION_SUCCESS, userMap);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    @RequestMapping("/updateProfilePhoto")
    public Object updateProfilePhoto(@RequestParam String id, @RequestParam String urlId){
        try{
            userService.updateProfilePhoto(Long.parseLong(id), urlId);
            return Result.success(Constant.API_OPERATION_SUCCESS);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    @RequestMapping("/updateName")
    public Object updateName(@RequestParam String id, @RequestParam String name){
        try{
            userService.updateName(Long.parseLong(id), name);
            return Result.success(Constant.API_OPERATION_SUCCESS);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    @RequestMapping("/upLoadBase64Jpg")
    public Object upLoadBase64Jpg(@RequestParam String base64File){
        try{
            Map<String, Object> parameterMap = new HashMap<>();
            parameterMap.put("imgStr", base64File);
//        Response response = Http.post2(webSystemIP + "/fileupload/base64toJpg", parameterMap, 1000 * 60);
//        Result result = Json.fromJson(Result.class, response.getContent());
            String result = HttpUtil.post(webSystemIP + "/fileupload/base64toJpg", parameterMap, 1000 * 60);
            return result;
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(Constant.API_NETWORK_ANOMALY);
        }
    }



}
